﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LiteQueryWPF.Controls
{
    /// <summary>
    /// DataPanel.xaml 的交互逻辑
    /// </summary>
    public partial class DataPanel : UserControl
    {
        Entities.DataSql data;
        public Entities.DataSql DataSql
        {
            get { return data; }
            set { data = value; }
        }

        public DataTable DataSource
        {
            get {
                var view = (dgvData.ItemsSource as DataView);
                if (view != null)
                    return view.Table;
                else
                    return null;
            }
            set
            {
                dgvData.ItemsSource = value.DefaultView;
                lblInfo.Content = string.Format("当前查询结果共 {0} 条记录", dgvData.Items.Count);
            }
        }

        public DataPanel()
        {
            InitializeComponent();
        }

        public DataPanel(Entities.DataSql d)
        {
            InitializeComponent();
            this.DataSql = d;
        }
    }

    public class TimetampConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value.GetType() == typeof(Int64)
                && Common.DateHelper.TimestampToDate((long)value) > new System.DateTime(1971, 1, 1))
                return LiteQueryWPF.Common.DateHelper.TimestampToDate((long)value);
            else
                return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                DateTime d = (DateTime)value;
            }
            catch {
                return value;
            }
            return LiteQueryWPF.Common.DateHelper.DateToTimestamp((DateTime)value);
        }
    }
}
